package net.board.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet("/BoardFrontController")

public class BoardFrontController extends javax.servlet.http.HttpServlet  implements javax.servlet.Servlet{
	private static final long serialVersionUID = 1L;//클래스에 대한 버전(이클립스가 자동으로 부여)
	
	//실제 사용자의 요청이 오면 반응할 메서드 get 방식으로 용청을 하건 post 방식으로 요청을 하건 무조건 호출
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 	System.out.println("모델 2테스트");
		 	//연결 테스트(url에 http://localhost:8080/121102_GeneralBoard/b.bo를 치게되면 콘솔에 프린트가 나온다.)
		 	
			String RequestURI=request.getRequestURI();//이전 페이지의 풀경로를 저장
			String contextPath=request.getContextPath();//이전 페이지의 마지막 주소 부분만 저장
			//전체주소에서 마지막주소 부분만 추출해서 command에 저장
			String command=RequestURI.substring(contextPath.length());
			ActionForward forward=null;//이동할 곳에 대한 정보를 저장할 객체
			Action action=null;//실제 작업을 수행할 객체
			
			if(command.equals("/BoardList.bo")){//이렇게 치면
				action = new BoardListAction();
				try{
					forward=action.execute(request, response);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			else if(command.equals("/BoardWrite.bo")){
				forward=new ActionForward();
				forward.setRedirect(false);
				forward.setPath("./board/qna_board_write.jsp");
			}
			else if(command.equals("/BoardAddAction.bo")){
				action  = new BoardAddAction();
				try {
					forward=action.execute(request, response );
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else if(command.equals("/BoardDetailAction.bo")){
				action = new BoardDetailAction();
				try{
					forward=action.execute(request, response);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			else if(command.equals("/BoardDelete.bo")){
				forward=new ActionForward();
				forward.setRedirect(false);
				forward.setPath("./board/qna_board_delete.jsp");
				
			}else if(command.equals("/BoardModify.bo")){
				   action = new BoardModifyView();
				   try{
					   forward=action.execute(request, response);
				   }catch(Exception e){
					   e.printStackTrace();
				   }
			}else if(command.equals("/BoardDeleteAction.bo")){
					action = new BoardDeleteAction();
					try{
						forward=action.execute(request, response);
					}catch(Exception e){
						e.printStackTrace();
					}
			
			}else if(command.equals("/BoardModify.bo")){
				   action = new BoardModifyView();
				   try{
					   forward=action.execute(request, response);
				   }catch(Exception e){
					   e.printStackTrace();
				   }
			}else if(command.equals("/BoardModifyAction.bo")){
				   action = new BoardModifyAction();
				   try{
					   forward=action.execute(request, response);
				   }catch(Exception e){
					   e.printStackTrace();
				   }
				   
			}else if(command.equals("/BoardReplyView.bo")){
				action = new BoardReplyView();
				try{
					forward=action.execute(request, response);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			else if(command.equals("/BoardReplyAction.bo")){
				action = new BoardReplyAction();
				try{
					forward=action.execute(request, response);
				}catch(Exception e){
					e.printStackTrace();
				}
			}



			
			if(forward != null){
				if(forward.isRedirect()){
					response.sendRedirect(forward.getPath());
				}else{
					RequestDispatcher dispatcher=
							request.getRequestDispatcher(forward.getPath());
					dispatcher.forward(request, response);
				}
			}
	}

	

	
	//get 방식
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}
	//post 방식
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

}